//
//  TodayView.m
//  Bracelet
//
//  Created by jing zhao on 2/12/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "HSBC_TodayView.h"

#import "APIHSBCTotal.h"

#define  MOVEDIFFERENT      68.0f
@interface HSBC_TodayView()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraint_topViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraint_containerViewHeight;
@property (weak, nonatomic) IBOutlet UIView *       ui_viewContainer;
@property (weak, nonatomic) IBOutlet UIView *       ui_top;
@property (weak, nonatomic) IBOutlet UILabel *      ui_labelTotalMeter;
@property (weak, nonatomic) IBOutlet UILabel *      ui_labelTotalMoney;
@property (weak, nonatomic) IBOutlet UILabel *      ui_labelmyCity;
@property (weak, nonatomic) IBOutlet UILabel *      ui_labelNextChange;
@property (weak, nonatomic) IBOutlet UILabel *      ui_labelTotalWalkers;
@property (weak, nonatomic) IBOutlet UILabel *      ui_labelTextYou;
@property (weak, nonatomic) IBOutlet UIWebView *    ui_webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *ui_activity;

@property (weak, nonatomic) IBOutlet UILabel *leadingDistance;
@property (weak, nonatomic) IBOutlet UILabel *targetCity;

@end


@implementation HSBC_TodayView


-(void)initWithType:(NSString*)type
{
    self.ui_labelmyCity.text =(NSString*)[[[NSUserDefaults standardUserDefaults]objectForKey:APIKEYUSERINFO]objectForKey:APIKEYCITYNAME];
    
    [self.ui_activity startAnimating];
//   [self updateNetwork];
  self.ui_webView.delegate = self;
    self.ui_webView.scrollView.scrollEnabled = NO;
 
}

#pragma network
-(void)updateNetwork
{
    UIRefreshTableView *refresh = (UIRefreshTableView*)self.superview;
  [APIHSBCTotal getHSBCTotalSuccess:^(APIHSBCTotal *hsbcTotal) {
    [self.ui_webView loadHTMLString:hsbcTotal.loadedHTML baseURL:nil];
    //self.ui_labelTotalWalkers.text = hsbcTotal.totalWalkers;
      [self setTotalWalkersLabel:hsbcTotal.totalWalkers];
    self.ui_labelNextChange.text = hsbcTotal.nextChallenge;
    self.targetCity.text = hsbcTotal.nextCityName;
    self.leadingDistance.text = hsbcTotal.distanceToNextCity;
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"we are %@ km to %@ currently", hsbcTotal.distanceToNextCity, hsbcTotal.nextCityName]];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Univers Next for HSBC W01 Rg" size:14.0] range:NSMakeRange(7, [hsbcTotal.distanceToNextCity length])];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(7, [hsbcTotal.distanceToNextCity length])];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Univers Next for HSBC W01 Rg" size:14.0] range:NSMakeRange(7+[hsbcTotal.distanceToNextCity length]+7, [hsbcTotal.nextCityName length])];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(7+[hsbcTotal.distanceToNextCity length]+7, [hsbcTotal.nextCityName length])];
    self.ui_labelTextYou.attributedText = str;

      [self.ui_activity stopAnimating];
      [refresh endRefreshData];
  } failure:^(NSError *error) {
       [refresh endRefreshData];
        [self.ui_activity stopAnimating];
  }];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
  
  NSString *urlstr = request.URL.absoluteString;
  NSRange range = [urlstr rangeOfString:@"ios://"];
  if (range.length!=0) {
    NSString *method = [urlstr substringFromIndex:(range.location+range.length)];
    SEL selctor = NSSelectorFromString(method);
    SuppressPerformSelectorLeakWarning([self performSelector:selctor withObject:nil]);
  }
  return YES;
}
-(void)loadingPage{
  NSLog(@"loadingpage !!!");
  
  [[NSNotificationCenter defaultCenter]postNotificationName:@"CharityWalkathon" object:nil];
  
}

//bottom  tableView

- (void)setTotalWalkersLabel:(NSString *)totalStr{
    NSMutableAttributedString *totalWalkersStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ walkers",totalStr]];
    [totalWalkersStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Univers Next for HSBC W01 Lt" size:28.0] range:NSMakeRange(0, [totalStr length]+1)];
    [totalWalkersStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [totalWalkersStr length])];
    
    [totalWalkersStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Univers Next for HSBC W01 Lt" size:16.0] range:NSMakeRange([totalStr length]+1,[totalWalkersStr length]-[totalStr length]-1 )];
    self.ui_labelTotalWalkers.attributedText = totalWalkersStr;

    
}

@end
